/**
 * 
 */
package org.hackystat.ui.orb.client.common;

import java.util.HashMap;
import java.util.Map;

import com.google.gwt.user.client.rpc.IsSerializable;

/**
 * @author David J. Nickles
 *
 */
public class Enum implements IsSerializable {
  private /* final */ String name;
  private /* final */ int ordinal;

  /**
   * Required no parameter constructor.
   */
  public Enum() {
    this("error", -1);
  }

  /**
   * Name and cardinal pair.
   * @param name the enumeration name.
   * @param ordinal the enumeration ordinal.
   */
  protected Enum(String name, int ordinal) {
    this.name = name;
    this.ordinal = ordinal;
  }

  /**
   * This method gets the associated name with this enumeration.
   * @return the associated name with this enumeration.
   */
  public final String getName() {
    return name;
  }

  /**
   * This method gets the cardinal associated with this enumeration.
   * @return the value associated with this enumeration.
   */
  public final int getOrdinal() {
    return ordinal;
  }

  /**
   * This method determines if two Enums are equal to each other.
   * @param o the other Enum.
   * @return true if equal, false if not.
   */
  public boolean equals(Object o) {
    if (this == o) {
      return true;
    }
    if (o == null) {
      return false;
    }
    if (o instanceof Enum) {
      Enum anEnum = (Enum) o;
      if (!name.equals(anEnum.name) || ordinal != anEnum.ordinal) {
        return false;
      }
      return true;
    }
    return false;
  }

  /**
   * This method returns a hash value for this Enum.
   * @return the hash code.
   */
  public int hashCode() {
    return ordinal;
  }

  /**
   * This method returns a Map<K,V> of the provided array of Enums.
   * @param enums an array of Enums.
   * @return a Map of the Enums.
   */
  protected static Map makeValuesMap(Enum[] enums) {
    Map map = new HashMap(enums.length);
    for (int i = 0; i < enums.length; i++) {
      map.put(enums[i].getName(), enums[i]);
    }
    return map;
  }
}

